from django.http import HttpResponse,HttpResponseRedirect


POST_FORM='''
<form method='post' action='/test_get_post'>
    用户名：<input type='text' name='uname'>
    <input type='submit' value='提交'>
</form>
'''

def page_2003_view(request):
    html="<h1>这是第一个页面</h1>"
    return HttpResponse(html)


def page1_view(request):
    html="<h1>这是第1个页面</h1>"
    return HttpResponse(html)



def page2_view(request):
    html="<h1>这是第2个页面</h1>"
    return HttpResponse(html)

def index_view(request):
    html="<h1>首页</h1>"
    return HttpResponse(html)

def pagen_view(request,pg):
    html="<h1>编号%s</h1>"%(pg)
    return HttpResponse(html)

def cal2_view(request,x,op,y):
    html="<h1>n:%s,op:%s,m:%s</h1>"%(x,op,y)
    return HttpResponse(html)

def cal_view(request,n,op,m):
    if(op not in ["add","sub","mul"]):
        return HttpResponse('op is wrong')
    result=0
    if(op=='add'):
        result=n+m
    elif(op=='sub'):
        result=n-m
    elif(op=='mul'):
        result=n*m
    html="<h1>答案为%s</h1>"%(result)
    return HttpResponse(html)

def test_request(request):
    print("method:",request.method)
    print("path_info",request.path_info)
    print("querystring is", request.GET)
    return  HttpResponseRedirect('page/1')
    #HttpResponse('test is ok')

def test_get_post(request):
    if (request.method=='GET'):
        print(request.GET['a'])
        print(request.GET.get('c','no_c'))
        print(request.GET.getlist('a'))
        return HttpResponse(POST_FORM)

    elif request.method=='POST':
        print(('uname is'),request.POST['uname'])
        return HttpResponse('post is ok')

    else:
        pass

    return HttpResponse('test is ok')

def test_html(request):
    # from django.template import loader
    # t=loader.get_template('test_html.html')
    # html =t.render()
    # return HttpResponse(html)
    from django.shortcuts import render
    dic={'username':'guoxiaonao','age':18}
    return render(request,'test_html.html',dic)

def say_hi():
    return "hahahahah"

class Dog:
    def say(self):
        return "i'm dog"

def test_html_param(request):
    from django.shortcuts import render
    dic={}
    dic['int']=88
    dic['str']='xcp'
    dic['lst']={'Tom','Jack','Lily'}
    dic['dict']={'a':9,'b':8}
    dic['func']=say_hi
    dic['class_obj']=Dog()
    return render(request,'test_html_param.html',dic)

def test_if_for(request):
    from django.shortcuts import render
    dic={}
    dic['x']=10
    dic['lst']={'xcp','sss','asdsa'}
    return render(request,'test_if_for.html',dic)

from django.shortcuts import render
def base_view(request):
    return render(request,'base.html')

def music_view(request):
    return render(request,'music.html')

def test_url(request):
    return render(request,'test_url.html')

def test_url_result(request):
    #302跳转
    from django.urls import reverse
    url=reverse('base_index')
    return HttpResponseRedirect(url)
    #return HttpResponse('----test url res is ok')